package com.cloudwise.pollux.user.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.cloudwise.pollux.common.request.PlaceOrderRequest;
import com.cloudwise.pollux.common.response.Response;
import com.cloudwise.pollux.user.rpc.OrderInterface;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

/**
 * @author sky
 * @date 2021/2/3 10:33 下午
 */
@Slf4j
@RestController
public class UserController {
    @Autowired
    private OrderInterface orderInterface;

    @Value("${server.port}")
    private Integer port;

    @Value("${spring.application.name}")
    private String name;


    @SentinelResource("userReg")
    @GetMapping(path = "/user/registerUser")
    public Response registerUser(String mobile){
        log.info("开始进行用户注册:{}",mobile);
        // TODO 模拟注册成功,并下单
        PlaceOrderRequest request = new PlaceOrderRequest();
        request.setGoodId("12333");
        request.setUserId(mobile);
        log.info("开始调用远程服务为新注册用户下单:{}",mobile);
        Response response = orderInterface.placeOrder(request);
        log.info("调用远程服务为新注册用户下单完毕,下单结果:{}",response);
        return response;

    }
    @SentinelResource("userLogin")
    @PostMapping("/user/login")
    public Response login(String mobile,String password){
        // TODO 模拟登录成功
        return Response.success(name+":"+port);
    }

}
